//==================================================
// D_NC_OW_electric_meter.ino
// 2015-02-06
//==================================================
// Nabton Client Generic Configuration
//==================================================

#define NFLOAT 2  // No of decimals i float value
#define SIDN  1   // No of SIDs
#define SID1 5
#define SID2 62
#define SID3 63
#define SID4 904
#define SID5 905
#define SID6 906
#define SID7 907
#define SID8 908

#define MAX_SID 10
#define MAX_ORDERS 100
char g_msg[120];
int g_sids[10] = {SIDN,SID1,SID2,SID3,SID4,SID5,SID6,SID7,SID8};
#define PIN_LED_STATUS 13

// Arduino-RPi protocol
#define NABTON_DATA     1 

//=================================================
//
// D0 RX used for serial communication to server (Raspberry Pi)
// D1 TX used for serial communication to server (Raspberry Pi)
// D2 
// D3 Message Pin (blink led)
// D4 
// D5 
// D6 
// D7 Led Pulse
// D8 
// D9 
// D10   
// D11  
// D12 
// D13
//
// A0
// A1
// A2
// A3
// A4 
// A5 
//=================================================
//==================================================
// Nabton Client Application Configuration
//==================================================
#define PIN_LED_PULSE 7
unsigned long ttemp;
unsigned long time1, time2, data;
//=================================================
void NB_printFloat(float value, int places) 
//=================================================
{
  int digit;
  float tens = 0.1;
  int tenscount = 0;
  int i;
  float tempfloat = value;
  float d = 0.5;
  
  if (value < 0) d *= -1.0;
  for (i = 0; i < places; i++) d/= 10.0;    
  tempfloat +=  d;
  if (value < 0) tempfloat *= -1.0;
  while ((tens * 10.0) <= tempfloat) 
  {
    tens *= 10.0;
    tenscount += 1;
  }
  if (value < 0) Serial.print('-');
  if (tenscount == 0) Serial.print(0, DEC);
  for (i=0; i< tenscount; i++) 
  {
    digit = (int) (tempfloat/tens);
    Serial.print(digit, DEC);
    tempfloat = tempfloat - ((float)digit * tens);
    tens /= 10.0;
  }
  if (places <= 0) return;
  Serial.print('.');  
  for (i = 0; i < places; i++) 
  {
    tempfloat *= 10.0; 
    digit = (int) tempfloat;
    Serial.print(digit,DEC);  
    tempfloat = tempfloat - (float) digit; 
  }
}
//=================================================
void NB_sendFloatData(int sid, float value)
//=================================================
{
    digitalWrite(PIN_LED_STATUS,HIGH);
    Serial.print(NABTON_DATA);Serial.print(" "); 
    Serial.print(sid);Serial.print(" "); 
    Serial.print(NFLOAT);Serial.print(" "); 
    NB_printFloat(value,NFLOAT);
    Serial.print(";");
    digitalWrite(PIN_LED_STATUS,LOW);
    //strcpy(dl[3],p);
} 
//======================================
void pulse()
//======================================
{ 
      time2 = time1;
      time1 = millis();
      data = time1 - time2;
      float watt = 3600.0/data*1000.0;
      digitalWrite(PIN_LED_PULSE,HIGH);
      delay(2);
      digitalWrite(PIN_LED_PULSE,LOW);
      NB_sendFloatData(SID1, watt);
}
//======================================
void setup()
//======================================
{
  Serial.begin(9600);
  pinMode(PIN_LED_PULSE,OUTPUT);
  pinMode(PIN_LED_STATUS,OUTPUT);
  digitalWrite(PIN_LED_STATUS,HIGH);
  attachInterrupt(0, pulse, FALLING);
}

//======================================
void loop()
//======================================
{
    delay(1000);
}
